import { request, title_manager } from '@/utils'

export default {
    namespaced: true,
    state: {
        loading: false,
        data: { avatar: '' }
    },
    mutations: {
        set_loading(state, payload) {
            state.loading = payload
        },
        set_data(state, payload) {
            state.data = payload
        },
    },
    actions: {
        async get_data(ctx) {
            if (ctx.state.data.avatar) {
                return;
            }
            ctx.commit('set_loading', true)
            const resp = await request.get('/api/setting')
            ctx.commit('set_data', resp)
            ctx.commit('set_loading', false)
            // 设置网站图标
            if (resp.favicon) {
                let link = document.querySelector('link[rel~="icon"]')
                if (link) {
                    link.href = resp.favicon
                } else {
                    link = document.createElement('link')
                    link.rel = 'icon'
                    link.href = resp.favicon
                    document.head.appendChild(link)
                }
            }
            // 设置网站标题
            if (resp.siteTitle) {
                title_manager.set_site_title(resp.siteTitle)
            }
        }
    }
}
